import cn.com.elite.Util;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

/**
 * @projectName: ngsapitask-hst
 * @package: cn.com.dev
 * @className: chatOAuth
 * @author: LiJT
 * @description: TODO
 * @date: 2024-02-20 15:11
 * @version: 1.0
 */
public class chatOAuth {
    private static final Log log = LogFactory.getLog(chatOAuth.class);

    public static String generateCodeChallenge(String codeVerifier) throws Exception {
        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        byte[] hashedBytes = digest.digest(codeVerifier.getBytes(StandardCharsets.UTF_8));

        // Base64 URL safe encoding without padding characters
        return Base64.getUrlEncoder().withoutPadding().encodeToString(hashedBytes);
    }

    public static void main(String[] args) {
        try {
            String codeVerifier = "xx123";
            // String codeVerifier = KHUtil.getRandomString(128);
            System.out.println("Generated codeVerifier: " + codeVerifier);

            String codeChallenge = generateCodeChallenge(codeVerifier);
            System.out.println("Generated codeChallenge: " + codeChallenge);

            // 获取用户信息
            String userinfoURL = "https://uat-identity.psdreamer.com/userinfo";

            // 获取用户信息
            Map<String, String> userinfoMap = new HashMap<>();
            userinfoMap.put("Authorization", "Bearer " + "FkF5pHQl3XifR5yf0KBo-tN9-m6QDLiBUmPcvXJ2KrWS84YhouDFZCd0bXlklxYoCeRvRJnrcMYlSxJpH8ItQ046F7vzjcCkNJXUaHEAyq681nZjDnSHVDosehPAlCxc");

            // 发送请求获取用户信息
            String userinfoRes = Util.doHttpGetUseHead(userinfoURL, "UTF-8", 10000, 0, userinfoMap);
            log.debug("发送请求获取用户信息 结果:" + userinfoRes);


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
